package com.cedifie.lendapp.ui.launch.impl

import android.content.Context
import com.cedifie.lendapp.analysis.TestAnalysisInit
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.launch.CedLaunchAct
import com.cedifie.lendapp.ui.launch.view.ICedLaunchView
import com.cedifie.lendapp.utils.helper.SharedHelper
import com.lib.base.utils.helper.ActivityStackManager
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/13 15:46
 * @Annotation：启动页 Presenter 实现
 */
class CedLaunchPresenterImpl(context: Context?) : MvpPresenter<ICedLaunchView>(context) {

    private var launchModel: CedLaunchModelImpl? = null
    init {
        launchModel = CedLaunchModelImpl()
    }

    /**
     * 获取地区转发
     */
    fun loadCedTimeZoneAction(){
        launchModel?.loadCedTimeZoneAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val dataString = data.optString("data")
                        val timeZone = dataString.split(",")[0]
                        val zoneCode = dataString.split(",")[1]
                        mvpView?.showCedTimeZoneRequestData(timeZone,zoneCode)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showCedToast(msg)
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取App统一配置信息
     */
    fun loadCedCommonConfigAction(){
        launchModel?.loadCedCommonConfigAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                //关闭所有页面
                ActivityStackManager.finishAllActivity(CedLaunchAct::class.java)
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        //保存所有数据
                        if (obj != null) {
                            SharedHelper.saveCedSPData(SharedHelper.CED_SPKEY.CEDIFIE_COMMON,obj.toString())
                        }
                        //已登录 - 进入主页
                        if (SharedHelper.getCedSPBoolean(SharedHelper.CED_SPKEY.CEDIFIE_LOGIN)){
                            //初始化analysis lib 赋值 用户id 和 token
                            TestAnalysisInit.initAnalysisLibAtLoginSuccess()
                            mvpView?.showCedMainPage()
                        } else {//进入登录页
                            mvpView?.showCedLoginPage()
                        }
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showCedToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
            }
        })
    }
}